package com.tuling.service;

import com.tuling.dao.UserDao;
import com.tuling.dao.UserDaoImpl;
import com.tuling.domain.User;

import java.util.List;

public class UserServiceImpl implements UserService{

    private UserDao userDao = new UserDaoImpl();

    @Override
    public User add(User user) {
        int insert = userDao.insert(user);
        if (insert == 1) {
            User login = login(user.getEmail(), user.getPwd());
            return login;
        }
        return null;
    }

    @Override
    public User login(String str, String pwd) {
        User userByStr = userDao.findByStr(str);
        if (userByStr == null) {
            return null;
        }
        if (pwd.equals(userByStr.getPwd())) {
            userByStr.setPwd("*****");
            return userByStr;
        }
        return null;
    }

    @Override
    public List<User> findAll() {
        return userDao.findALl();
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int del(int id) {
        return userDao.del(id);
    }

    @Override
    public User findById(int id) {
        return userDao.findById(id);
    }
}
